package yyds.sniarbtej.transformer;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;

public abstract class BaseClassTransformer implements ClassTransformer {
    public final boolean canTransform(String className) {
        return className.equals(this.canTransformClassName());
    }

    public static byte[] transform(byte[] in, ClassRewriter classRewriter) {
        ClassReader reader = new ClassReader(in);
        ClassNode node = new ClassNode(ASM_VERSION);
        reader.accept(node, 0);

        classRewriter.addMethods(node.methods);

        //DebugUtil.log("Input: " + node.name);
        for (MethodNode methodNode : node.methods) {
            classRewriter.rewriteMethod(methodNode);
        }

        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
        node.accept(writer);
        return writer.toByteArray();
    }
}
